package com.sghd.candy.module.charge.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.time.DateUtils;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;

/**
 * @author frank
 * Charge
 */
@Entity
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_MINUTE))
@Table(appliesTo = "Charge", indexes = {
        @Index(name = "IDX_VIP_CHARGE_START_DATE", columnNames = "firstChargeTime"),
        @Index(name = "IDX_VIP_CHARGE_END_DATE", columnNames = "lastChargeTime")})
@javax.persistence.Table(catalog = "bsdld")
public class Charge implements IEntity<Long> {

    /** 玩家标识*/
    @Id
    private Long id;
    /** 累计充值(消费)*/
    private int total;
    /**今日充值*/
    private int dayCharge;
    /** 首冲时间*/
    private Date firstChargeTime;
    /** 最后一次充值时间*/
    private Date lastChargeTime;

    @Override
    public Long getId() {
        return id;
    }

    void setId(Long id) {
        this.id = id;
    }

    public int getTotal() {
        return total;
    }

    void setTotal(int total) {
        this.total = total;
    }

    public int getDayCharge() {
        return dayCharge;
    }

    void setDayCharge(int dayCharge) {
        this.dayCharge = dayCharge;
    }

    public Date getFirstChargeTime() {
        return firstChargeTime;
    }
    @Enhance
    public void setFirstChargeTime(Date firstChargeTime) {
        this.firstChargeTime = firstChargeTime;
    }

    public Date getLastChargeTime() {
        return lastChargeTime;
    }

    public void setLastChargeTime(Date lastChargeTime) {
        this.lastChargeTime = lastChargeTime;
    }

    @Enhance
    public void recharge(int fee) {
        if (DateUtils.isToday(getLastChargeTime())) {
            setDayCharge(getDayCharge() + fee);
        } else {
            setDayCharge(fee);
        }

        setTotal(getTotal() + fee);
        setLastChargeTime(new Date());
    }
    public static Charge valueOf(long playerId) {
        Charge charge = new Charge();
        charge.setTotal(0);
        charge.setTotal(0);
        charge.setId(playerId);
        return charge;
    }
}